PLUGIN.Name = "Keyboard"
PLUGIN.DefaultOn = false
PLUGIN.Description = "Plays keystroke sounds while typing on the chat."

function PLUGIN:Load()
	self.Length = 0
	self.LastUsed = 1
	self.LastTime = 0
end

-- Functions that begin with a "_" are not automatically hooked.
function PLUGIN:_DiceNoRepeat( myMax, lastUsed )
	local dice = math.random(1, myMax - 1)
	if (dice >= lastUsed) then
		dice = dice + 1
	end
	
	return dice
end

-- This function is automatically hooked on Mount, and unhooked on Unmount.
function PLUGIN.HOOK:ChatTextChanged( sText )
	if RealTime() < (self.LastTime + 0.12) then return end
	self.LastTime = RealTime()

	local sound = "ambient/machines/keyboard"
	
	local length = string.len(sText)
	if length < self.Length then
		sound = sound .. "7_clicks_enter.wav"
		
	else
		local dice = self:_DiceNoRepeat(6, self.LastUsed)
		self.LastUsed = dice
		sound = sound .. dice .."_clicks.wav"
		
	end
	self.Length = length
	
	LocalPlayer():EmitSound( sound )
	
end

-- This function is automatically hooked on Mount, and unhooked on Unmount.
function PLUGIN.HOOK:FinishChat()
	self.Length = 0
	
	local sound = "ambient/machines/keyboard7_clicks_enter.wav"
	LocalPlayer():EmitSound( sound )
end

function PLUGIN:Unload()
end
